# Create your views here

from django.http import HttpResponseRedirect
from django.contrib import auth
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import render, get_object_or_404
from sign.models import Event
from sign.models import Guest


def index(request):
    return render(request, 'index.html')


def login_action(request):
    if request.method == 'POST':
        username = request.POST.get('username', '')
        password = request.POST.get('password', '')
        user = auth.authenticate(username=username, password=password)
        if user is not None:
            auth.login(request, user)    # 登录
            response = HttpResponseRedirect('/event_manage/')
            request.session['username'] = username      # 将session信息记录到浏览器
            return response
        else:
            return render(request, 'index.html', {'error': 'username or password error!'})


@login_required
def event_manage(request):
    event_list = Event.objects.all()
    username = request.session.get('username', '')     # 读取浏览器session
    paginator = Paginator(event_list, 10)
    page = request.GET.get('page')
    try:
        contacts = paginator.page(page)
    except PageNotAnInteger:
        # 如果page不是整数，取第一页
        contacts = paginator.page(1)
    except EmptyPage:
        # 如果page不在范围，取最后一页
        contacts = paginator.page(paginator.num_pages)
    return render(request, 'event_manage.html', {'username': username, 'events': contacts})


@login_required
def guest_manage(request):
    guest_list = Guest.objects.all()
    username = request.session.get('username', '')  # 读取浏览器session
    paginator = Paginator(guest_list, 10)
    page = request.GET.get('page')
    try:
        contacts = paginator.page(page)
    except PageNotAnInteger:
        # 如果page不是整数，取第一页
        contacts = paginator.page(1)
    except EmptyPage:
        # 如果page不在范围，取最后一页
        contacts = paginator.page(paginator.num_pages)
    return render(request, 'guest_manage.html', {'username': username, 'guests': contacts})


@login_required
def search_event(request):
    username = request.session.get('username', '')
    search_event = request.GET.get('search_event', '')
    event_list = Event.objects.filter(name__contains=search_event)
    return render(request, 'event_manage.html', {'username': username, 'events': event_list})


@login_required
def search_guest(request):
    username = request.session.get('username', '')
    search_guest = request.GET.get('search_guest', '')
    guest_list = Guest.objects.filter(realname__contains=search_guest)
    return render(request, 'guest_manage.html', {'username': username, 'guests': guest_list})


@login_required
def sign_index(request, eid):
    username = request.session.get('username', '')
    event = get_object_or_404(Event, id=eid)
    return render(request, 'sign_index.html', {'username': username, 'event': event})


@login_required
def sign_index_action(request, eid):
    username = request.session.get('username', '')
    event = get_object_or_404(Event, id=eid)
    phone = request.POST.get('phone', '')
    result = Guest.objects.filter(phone=phone)
    if not result:
        return render(request, 'sign_index.html', {'username': username, 'event': event, 'hint': 'phone error'})
    result = Guest.objects.filter(phone=phone, event_id=eid)
    if not result:
        return render(request, 'sign_index.html', {'username': username, 'event': event,
                                                   'hint': 'event id or phone error'})
    result = Guest.objects.get(phone=phone, event_id=eid)
    if result.sign:
        return render(request, 'sign_index.html', {'username': username, 'event': event,
                                                   'hint': 'user has sign in'})
    else:
        Guest.objects.filter(phone=phone, event_id=eid).update(sign='1')
        return render(request, 'sign_index.html', {'username': username, 'event': event,
                                                   'hint': 'sign in success', 'guest': result})

@login_required
def logout(request):
    auth.logout(request)
    response = HttpResponseRedirect('/index/')
    return response
